//
//  KDMemoController.m
//  KongDays
//
//  Created by deeplu on 13. 1. 6..
//  Copyright (c) 2013년 BlueBeans. All rights reserved.
//

#import "KDMemoController.h"

#define kMemoTextViewTag    100

@interface KDMemoController ()

- (UITextView *)memoTextView;
- (void)done;

@end

@implementation KDMemoController
@synthesize memo;
@synthesize delegate;
@synthesize memoTable;

- (id)init
{
    self = [super initWithNibName:@"KDMemoController" bundle:nil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [memo release];
    [memoTable release];
    
    [super dealloc];
}

#pragma mark UIViewController methods

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"Memo", nil);
    
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
                                   initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                   target:self
                                   action:@selector(done)];
    
    self.navigationItem.rightBarButtonItem = doneButton;
    [doneButton release];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self memoTextView].text = self.memo;
    [[self memoTextView] becomeFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - UITableViewDelegate methods

-       (CGFloat)tableView:(UITableView *)tableView
   heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180.0f;
}

#pragma mark - UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section;
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]
                 initWithStyle:UITableViewCellStyleDefault
                 reuseIdentifier:CellIdentifier] autorelease];
        
        UITextView *memoTextView = [[UITextView alloc]
                                    initWithFrame:CGRectMake(5.0, 10.0, 290.0, 165.0)];
        memoTextView.tag = kMemoTextViewTag;
        memoTextView.backgroundColor = [UIColor clearColor];
        memoTextView.font = [UIFont fontWithName:@"Helvetica" size:14.0f];
        memoTextView.text = self.memo;
        
        [cell.contentView addSubview:memoTextView];
        
        [memoTextView release];
    }
    
    return cell;
}

#pragma mark Private methods

- (UITextView *)memoTextView
{
    UITableViewCell *cell = [self.memoTable cellForRowAtIndexPath:
                             [NSIndexPath indexPathForRow:0 inSection:0]];
    
    return (UITextView *)[cell viewWithTag:kMemoTextViewTag];
}

- (void)done
{
    [[self memoTextView] resignFirstResponder];
    
    self.memo = [self memoTextView].text;
    
    [self.delegate memoDidChange:self.memo];
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
